$(function () {
    // 右侧导航栏动画
    (function () {
        $(window).resize(function () {
            var offsetTop = $(window).height() - 70;
            console.log(offsetTop);
            $(".rightnavwrap").css("height", offsetTop);
            $(".rightcont").css("height", offsetTop);

        });
    }());
    (function () {
        $(".rigtbox").hover(function () {
            $(this).children(".tip").stop().css({
                "width": "86px",
                "left": "-88px",
            });
            $(this).children(".tipcode").stop().css({
                "width": "150px",
                "left": "-152px",
            });
            $(this).addClass("rigtbox1");
        }, function () {
            $(this).children(".tip").stop().css({
                "width": "0px",
                "left": "0px",
            });
            $(this).children(".tipcode").stop().css({
                "width": "0px",
                "left": "-152px",
            });
            $(this).removeClass("rigtbox1");
        })
    }());
    //用户信息点击事件
    (function () {
        var flage = true;
        $(".rigtbox").click(function () {
            $(this).addClass("rigtbox2").siblings(".rigtbox").removeClass("rigtbox2");
        })
        $(".myinfo").click(function () {
            if (flage) {
                $(".rightcont").show("linear", function () {
                    $(".login-ban").fadeIn().siblings().fadeOut();
                });
                flage = false;
            } else {
                $(".rightcont").hide("linear");
                flage = true;
            }
        });

        $(".cart").click(function () {
            $(".rightcont").show("linear", function () {
                $(".cart-ban").fadeIn().siblings().fadeOut();
            });
            // if(!flage){
            //     $(".cart-ban").fadeIn().siblings().fadeOut();
            // }
        });

        $(".rightarrow").click(function () {
            $(".rightcont").toggle("linear", function () {
                $(".login-ban").fadeOut();
            });
        })
        $(window).on('scroll', function () {
            var scroll = $(window).scrollTop();
            if (scroll > 300) {
                $(".returnTop").css("display", "block");
                $(".returnTop").click(function () {
                    $('body,html').stop().animate({
                        scrollTop: 0
                    });
                });
            } else {
                $(".returnTop").css("display", "none");
            }
        })
        //右侧导航购物车
        $(".lookCart").click(function () {
            window.location.href = "./MF-cart.html";
        })
    }());
    (function () {

        //从数据库中取出要加入购物车的商品ID
        var tolCount = 0;
        var tolPrice = 0;
        var html = "";
        $.ajax({
            url: '../../php/MF-php/MF-searchCart.php',
            type: 'get',
            dataType: 'json',
            error: function () {
                console.log("数据请求失败！");
            },
            success: function (res) {
                console.log(res)
                tolCount = res.length;
                for (var i = 0; i < res.length; i++) {
                    tolPrice += parseFloat(res[i].price);
                    var comId = res[i].type_id;
                    rightCartCom(comId)
                }
                $("#txtTotalNum").html(tolCount);
                $(".redDot>span").html(tolCount);
                $("#txtTotalPrice").html(tolPrice.toFixed(2))
            }
        })
        //取出当前点击的商品信息
        function rightCartCom(comId) {
            $.ajax({
                url: '../../php/MF-php/MF-cart.php',
                type: 'get',
                dataType: 'json',
                data: {
                    comId: comId
                },
                error: function () {
                    console.log("数据请求失败！");
                },
                success: function (res) {
                    console.log(res);
                    var str1 = res[0].price;
                    var pro_price = parseFloat(str1.slice(1));
                    // var productor = res[0].pro_assess;
                    tolPrice = tolPrice + pro_price;
                    html += `
          <li>
          <div class="rigImg">
            <img src="${res[0].img_url}" alt="图片">
          </div>
          <div class="rightIntroduct">
            <div><span>${res[0].pro_title}</span></div>
            <div>
              <span>${pro_price}</span>
              <span>x</span>
              <span>1</span>
            </div>
          </div>
          <div class="rightPrice">
            <span>￥</span>
            <span>${pro_price}</span>
          </div>
        </li>
          `;
                    $(".product_show").html(html);
                    // $("#txtTotalNum").html(tolCount);
                    // $(".redDot>span").html(tolCount);
                    // $("#txtTotalPrice").html(tolPrice.toFixed(2))
                }

            })
        }
    }())
});